package org.zhenhu.CSCE310.StrassenMatrix;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;

import Jama.Matrix;


public class Readfile {
	
	public static double[][] readFile(String fileName){
		Scanner read = null;
		
		try{
			read = new Scanner(new File(fileName));
		} catch(FileNotFoundException e){
			e.printStackTrace();
		}
	
		String firstLine = read.nextLine();
		String[] firstLineToken = firstLine.split(" ");
		int size = Integer.parseInt(firstLineToken[0]);
		
		double[][] array= new double [size][size];
		
		String line;
		String[] lineToken;
		int row = 0;
		while(read.hasNextLine()&& row <size){
			
			line = read.nextLine();
			lineToken = line.split(" ");
			for(int col = 0; col < size; col++){
					array[row][col]=Double.parseDouble(lineToken[col]);
//					System.out.print(array[row][col]+ ",");
				}
//			System.out.println();
				row++;
		}
		
		
		read.close();
		return array;
		}
			
		
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Readfile test = new Readfile();
		double[][] array1 = test.readFile(args[0]);
		double[][] array2 = test.readFile(args[1]);
//		System.out.println(Arrays.deepToString(array1));
//		System.out.println(Arrays.deepToString(array2));
		StrassenMatrix sm = new StrassenMatrix();
		Matrix matrix1 = new Matrix(array1);
		Matrix matrix2 = new Matrix(array2);
		Matrix m1 = sm.padding(matrix1);
		Matrix m2 = sm.padding(matrix2);
		Matrix result = sm.strassen(m1, m2);
		Matrix rs = sm.resultProcess(result, matrix1, matrix2);
		double[][] rsArray = rs.getArray();
		
		DecimalFormat f = new DecimalFormat("####################################.00");
		for(int i=0; i < rsArray.length;i++){
			for(int j =0; j < rsArray.length; j++){
				
				rsArray[i][j] = Double.parseDouble(f.format(rsArray[i][j]));
			}
		}
		
		Matrix normal=matrix1.times(matrix2);
		double[][] rsnormal = normal.getArray();
		DecimalFormat format = new DecimalFormat("####################################.00");
		
		for(int i=0; i < rsnormal.length;i++){
			for(int j =0; j < rsnormal.length; j++){
				
				rsnormal[i][j] = Double.parseDouble(format.format(rsnormal[i][j]));
			}
		}
		
		System.out.println("MatrixC (normal multiplication):");
		for(int i =0; i< rsnormal.length;i++){
			System.out.println(Arrays.toString(rsnormal[i]));
		}
		
		
		
		System.out.println("MatrixC (Strassen multiplication):");
		for(int i =0; i< rsArray.length;i++){
			System.out.println(Arrays.toString(rsArray[i]));
		}
	}

}
